package com.weitu.environmental.solidWaste.controller;


import com.weitu.base.controller.BaseController;
import com.weitu.environmental.solidWaste.entity.SolidWasteStorageView;
import com.weitu.environmental.solidWaste.entity.StorageSource;
import com.weitu.environmental.solidWaste.service.SolidWasteStorageViewService;
import com.weitu.paging.DataGridPagination;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.util.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangqingxi 2017-03-31
 */
@Controller
@RequestMapping("environmental/solidWaste/storageView")
public class SolidWasteStorageViewController extends BaseController {

    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "environmental/solidWaste/storageView";

    private final SolidWasteStorageViewService solidWasteStorageViewService;

    @Autowired
    public SolidWasteStorageViewController(SolidWasteStorageViewService solidWasteStorageViewService) {
        this.solidWasteStorageViewService = solidWasteStorageViewService;
    }


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView listView() {

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName(MODULE_BATH_PATH + "/list");

        return modelAndView;
    }

    @RequestMapping(value = "/storageList", method = RequestMethod.GET)
    public ModelAndView storageListView(String solidWasteId, String hazardousWasteCodeId) {

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("solidWasteId", solidWasteId);
        modelAndView.addObject("hazardousWasteCodeId", hazardousWasteCodeId);

        modelAndView.setViewName(MODULE_BATH_PATH + "/storageList");

        return modelAndView;
    }
    @RequestMapping(value = "/disposeStorageList", method = RequestMethod.GET)
    public ModelAndView disposeStorageListView(String solidWasteId, String hazardousWasteCodeId) {

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("solidWasteId", solidWasteId);
        modelAndView.addObject("hazardousWasteCodeId", hazardousWasteCodeId);

        modelAndView.setViewName(MODULE_BATH_PATH + "/disposeStorageList");

        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/findByParam")
    public Object findByParam(HttpServletRequest request) {


        Map<String, Object> paramMap = new HashMap<String, Object>();

        //获取前台参数
        putParametersIntoMap(paramMap);

        Integer totalNum = solidWasteStorageViewService.findByParamCount(paramMap);//获取总数
        DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);

        PageView pageView = pagination.getPageView();//生成pageView分页对象
        paramMap.put("pageView", pageView);

        List<SolidWasteStorageView> solidWasteStorageViewList = solidWasteStorageViewService.findByParam(paramMap);

        return pagination.flushDataGridData(solidWasteStorageViewList);
    }

    @ResponseBody
    @RequestMapping("/findStorageSourceByParam")
    public Object findStorageSourceByParam(HttpServletRequest request) {


        Map<String, Object> paramMap = new HashMap<String, Object>();

        //获取前台参数
        putParametersIntoMap(paramMap);

        Integer totalNum = solidWasteStorageViewService.findStorageSourceByParamCount(paramMap);//获取总数
        DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);

        PageView pageView = pagination.getPageView();//生成pageView分页对象
        paramMap.put("pageView", pageView);

        List<StorageSource> storageSourceList = solidWasteStorageViewService.findStorageSourceByParam(paramMap);

        return pagination.flushDataGridData(storageSourceList);
    }

    @ResponseBody
    @RequestMapping("/findEntityByParams")
    public SolidWasteStorageView findEntityByParams() {

        Map<String, Object> paramMap = new HashMap<String, Object>();

        //获取前台参数
        putParametersIntoMap(paramMap);

        return solidWasteStorageViewService.findEntityByParams(paramMap);
    }

    /**
     * 处理返回参数
     *
     * @param result 返回值
     * @return 返回值
     */
    private AjaxResult resultAjaxResult(Integer result) {
        AjaxResult ajaxResult = new AjaxResult();
        if (result > 0) {
            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("操作成功");
        }
        return ajaxResult;
    }

    private void addParamToParamList(List<Map> paramList, String swaId) {
        for (Map map : paramList) {
            map.put("swaId", swaId);
            map.put("id", IDGenerator.getId());
        }
    }
}
